<?php
	require_once("../inc/function.php");
	
function GetOrders()
{
	$conn = GetConnection();
    return $conn->query("SELECT * FROM  `Order` ");   	
}
function NewOrder($row)
{
	$customer = $row['customer_id'];
	$amount = $row['amount'];	
	$address = $row['address'];
	$city = $row['city'];
	$state = $row['state'];
	$zip = $row['zip'];
	$country = $row['country'];
	$phone = $row['phone'];
	$tax = $row['tax'];
	$email = $row['email'];
	$date = date("Y:m:d H:m:s");
	$tracking = rand_string(20);	
	$conn = GetConnection();
	$conn->query("INSERT INTO `Order` Values(NULL, '$customer', '$amount', '$address', '$city', '$state', '$zip', '$country', '$phone', '$tax', '$email', '$date', '$tracking')");
	$conn->close();	
}
function rand_string( $length ) 
{
	$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";	

	$size = strlen( $chars );
	for( $i = 0; $i < $length; $i++ ) 
	{
		$str .= $chars[ rand( 0, $size - 1 ) ];
	}
	return $str;
}
function GetOrder($id)
{
	$conn = GetConnection();
    $sql = "SELECT * FROM `Order` WHERE id='$id'";
    $results = $conn->query($sql);
    //echo $sql;
    $row = $results->fetch_assoc();
    $conn->close();
    return $row;	
}
function SaveOrder($row)
{	
	$conn = GetConnection();
	$id = $row['id'];
	$customer = $row['customer_id'];
	$amount = $row['amount'];	
	$address = $row['address'];
	$city = $row['city'];
	$state = $row['state'];
	$zip = $row['zip'];
	$country = $row['country'];
	$phone = $row['phone'];
	$tax = $row['tax'];
	$email = $row['email'];
	if(strlen($email)>0)
	{
		   $conn->query("UPDATE `Order` SET Customer_id='$customer', amount ='$amount', address ='$address', city ='$city', state='$state', zip='$zip', country='$country', phone='$phone', tax='$tax', email='$email' WHERE id='$id' ") or die(mysql_error());        
	}
	else 
	{		
		header("Location: ../Orders/edit.php");
	}		  
       $conn->close();       
}
function DeleteOrder($id)
{
	$conn = GetConnection();
	$results = $conn->query("DELETE FROM `Order` WHERE id=$id");
	header('Location: http://cs.newpaltz.edu/~N01852322/Final/Orders/');
}

function GetCustomers()
{
        $conn = GetConnection();
        return $conn->query('SELECT * FROM Customer');
}
?>